﻿namespace MCP23017_NetduinoPlus {
  using System.Threading;
  using ExpressIS.NetduinoDeviceLibrary;
  using Microsoft.SPOT.Hardware;

  public class Program {
    public static void Main() {
      // Setup -
      //  1. Hookup the MCP23017 as directed in the driver code summary
      //  2. Hookup an LED to the GPA0 pin (pin 21 on the MCP23017) and ground (+ resistor as required)

      // Create a shared I2CDevice that all drivers will use (we don't care about the configuration right now).
      I2CDevice sharedI2C = new I2CDevice(new I2CDevice.Configuration(0, 0));

      // Create aninstance of our device
      using (MCP23017 device = new MCP23017(sharedI2C)) {
        // Initialize the device
        device.InitializeDevice();

        // Create an output port that will blink the LED0 or LED1
        IOutputPort port0A = device.CreateOutputPort(Cpu.Pin.GPIO_Pin0, false);
        IOutputPort port1A = device.CreateOutputPort(Cpu.Pin.GPIO_Pin1, false);

        // Create an input port that will signal the LED0
        IInputPort port2A = device.CreateInputPort(Cpu.Pin.GPIO_Pin2, false, Port.ResistorMode.PullUp);

        // Create an interrupt input port that can signal LED1
        IInterruptPort port3A = device.CreateInterruptPort(Cpu.Pin.GPIO_Pin3, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh);
        port3A.OnInterrupt += (uint port, uint state, System.DateTime time) => 
        {
          //port1A.Write(data1);



        };

        // Start blinking
        while (true) {
          port0A.Write(port2A.Read());
          Thread.Sleep(50);
        }
      }
    }
  }
}
